﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ClientRegister
{
    public partial class newStudentRegisterCourseForm : Form
    {

        string[] idClass;

        SynchronizeData sync = new SynchronizeData();

        public newStudentRegisterCourseForm()
        {
            InitializeComponent();
        }

        private void newStudentRegisterCourse_Load(object sender, EventArgs e)
        {

            ////Truoc khi thuc hien thao tac dang ky thi can thuc hien viec cap nhat du lieu vao database
            ////Nhan du lieu Hoc vien trong cot InsertData tu webservice

            //sync.GetDataForInsertMethodWithID(sync.studentTb, sync.insertcol);

            ////Nhan du lieu hoc vien trong cot UpdateData tu webservice
            //sync.GetDataForUpdateMethodWithID(sync.studentTb, sync.updatecol);

            ////Nhan du lieu thong tin lop hoc cua hoc vien tu server trong cot insert
            //sync.GetDataForInsertMethodWithNoneID(sync.ClassOfStu, sync.insertcol);

            ////Nhan du lieu thon tin hoc vien trong cot update tu server
            //sync.GetDataForUpdateMethodWithNoneID(sync.ClassOfStu, sync.updatecol);

            loadComboGender();

            CourseDataProvider course = new CourseDataProvider();
            DataSet dscourse = course.loadCourses();
            cbbCourse.DataSource = dscourse.Tables[0];
            cbbCourse.DisplayMember = "NameCourse";
            cbbCourse.ValueMember = "ID";
        }


        public void loadComboGender()
        {
            GenderDataProvider gender = new GenderDataProvider();
            DataSet ds = gender.loadDs();
            comboBox_Gender.DataSource = ds.Tables[0];
            comboBox_Gender.DisplayMember = "Gender";
            comboBox_Gender.ValueMember = "IDGender";
        }

        private void button2_Click(object sender, EventArgs e)
        {

            ////Truoc khi thuc hien thao tac dang ky thi can thuc hien viec cap nhat du lieu vao database
            ////Nhan du lieu Hoc vien trong cot InsertData tu webservice
            
            //sync.GetDataForInsertMethodWithID(sync.studentTb, sync.insertcol);

            ////Nhan du lieu hoc vien trong cot UpdateData tu webservice
            //sync.GetDataForUpdateMethodWithID(sync.studentTb, sync.updatecol);

            ////Nhan du lieu thong tin lop hoc cua hoc vien tu server trong cot insert
            //sync.GetDataForInsertMethodWithNoneID(sync.ClassOfStu, sync.insertcol);

            ////Nhan du lieu thon tin hoc vien trong cot update tu server
            //sync.GetDataForUpdateMethodWithNoneID(sync.ClassOfStu, sync.updatecol);

            string name = txtFullName.Text.ToString();
            string birthday = txtBirthday.Text.ToString();
            string job = txtJob.Text.ToString();
            string email = txtEmail.Text.ToString();
            int phone = int.Parse(txtPhone.Text.ToString());
            string address = txtAddress.Text.ToString();

            int idgender = int.Parse(comboBox_Gender.SelectedValue.ToString());

            GetNextID getId = new GetNextID();
            string lasId = getId.getLastID(getId.studentTB, getId.IDCol);
            string nextId = getId.NextID(lasId, getId.prefixID);

            StudentDataProvider std = new StudentDataProvider();
            int result = std.insertStudent(nextId, name, idgender, birthday, job, phone, email, address);


            //string xmlStu = std.SelectStudentsAndConvertToXMLString(idStu);
            ////Chuyen thong tin hoc vien len server
            //sync.transfDataForInsertMethod(xmlStu, sync.studentTb);

            int result2 = 0;
            //string xmlStuClass = "";
            StudentInClassDataProvider stdClass = new StudentInClassDataProvider();
           
            for (int i = 0; i < idClass.Length; i++)
            {
                result2 = stdClass.InsertStudentInClass(nextId, idClass[i]);
                if (result2 == 0) break;
                //xmlStuClass += stdClass.selectClassOfStu(lastId, idClass[i]);
            }
            ////Chuyen thong tin lop hoc cua hoc vien len server
            //sync.transfDataForInsertMethod(xmlStuClass, sync.ClassOfStu);

            if (result2 != 0)
                MessageBox.Show("Đăng ký thành công");
            else MessageBox.Show("Đăng ký không thành công");
        }

        private void button_Clear_Click(object sender, EventArgs e)
        {
            txtAddress.Clear();
            txtBirthday.Clear();
            txtEmail.Clear();
            txtFullName.Clear();
            txtJob.Clear();
            txtPhone.Clear();

            ////Nhan du lieu Hoc vien trong cot InsertData tu webservice

            //sync.GetDataForInsertMethodWithID(sync.studentTb, sync.insertcol);
            ////Nhan du lieu hoc vien trong cot UpdateData tu webservice
            //sync.GetDataForUpdateMethodWithID(sync.studentTb, sync.updatecol);

            ////Nhan du lieu thong tin lop hoc cua hoc vien tu server trong cot insert
            //sync.GetDataForInsertMethodWithNoneID(sync.ClassOfStu, sync.insertcol);

            ////Nhan du lieu thon tin hoc vien trong cot update tu server
            //sync.GetDataForUpdateMethodWithNoneID(sync.ClassOfStu, sync.updatecol);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbbCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Display ngay khai giang
            if (cbbCourse.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else{
                CourseDataProvider courseinfo = new CourseDataProvider();
                SqlDataReader sdrCourse = courseinfo.loadCourseInfo(cbbCourse.SelectedValue.ToString());
                if (sdrCourse.Read())
                    txtKG.Text = DateTime.Parse(sdrCourse.GetValue(2).ToString()).ToShortDateString();
                sdrCourse.Close();
            }
            //Display thong tin lop hoc ra DataGridView
            if (cbbCourse.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                ClassDataProvider clsList = new ClassDataProvider();
                DataSet dsClass = clsList.loadClass(cbbCourse.SelectedValue.ToString());
                DataTable dtClass = dsClass.Tables[0];
                GridListClass.DataSource = dtClass;

                idClass = new string[dtClass.Rows.Count];

                for (int i = 0; i < dtClass.Rows.Count; i++)
                {
                    idClass[i] = dtClass.Rows[i].ItemArray[0].ToString();
                }
                //Tinh tong hoc phi
                int totalFee = clsList.loadTotalFee(cbbCourse.SelectedValue.ToString());
                txtTotalFee.Text = totalFee.ToString();
            }
        }
    }
}
